package com.cqhilink.iot.jobstandardmanagement.services.innerservice.impl.regionalmaintenance;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cqhilink.api.common.utils.UniqueUtil;
import com.cqhilink.api.common.utils.date.DateStyle;
import com.cqhilink.api.common.utils.date.DateUtil;
import com.cqhilink.iot.jobstandardmanagement.facade.utils.TreeIconSkin;
import com.cqhilink.iot.jobstandardmanagement.dao.mapper.RegionMapper;
import com.cqhilink.iot.jobstandardmanagement.dao.mapper.RegionalMaintenanceMapper;
import com.cqhilink.iot.jobstandardmanagement.facade.model.Employee;
import com.cqhilink.iot.jobstandardmanagement.facade.model.Region;
import com.cqhilink.iot.jobstandardmanagement.facade.model.RegionalMaintenance;
import com.cqhilink.iot.jobstandardmanagement.facade.model.Ztree;
import com.cqhilink.iot.jobstandardmanagement.facade.service.regionalmaintenance.RegionalMaintenanceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by huangxueqian on 2017/4/19/019.
 * Modify by huangxueqian on 2017/08/09 改版区域信息 ， 将巡检点 作为区域进行巡检
 */
public class RegionalMaintenanceServiceImpl implements RegionalMaintenanceService {

    @Autowired
    private RegionalMaintenanceMapper regionalMaintenanceMapper;

    @Autowired
    private RegionMapper regionMapper;

    /**
     * @param regionalMaintenance 区域信息 查询 列表
     * @return
     * @throws Exception
     */
    @Override
    public JSONObject queryOrgRegion(RegionalMaintenance regionalMaintenance) throws Exception {
        JSONObject jsonObject = new JSONObject();
        Integer total = regionalMaintenanceMapper.queryCount(regionalMaintenance);
        jsonObject.put("total",total);
        jsonObject.put("rows",JSON.toJSON(regionalMaintenanceMapper.queryTable(regionalMaintenance)));
        return jsonObject ;
    }
    /**
     * @param regionIds  区域信息基本信息Id
     * @param orgId  区域信息所属组织
     * @param employee 操作人员
     */
    @Override
    public int insertRegionalMaintenanceList(List<String> regionIds, String orgId, Employee employee) throws Exception {
        int resInt = 0;
        if (!StringUtils.isEmpty(regionIds) && !StringUtils.isEmpty(orgId)) {
            for (String regionId : regionIds) {
                RegionalMaintenance regionalMaintenance = new RegionalMaintenance();
                Region region = regionMapper.selectByPrimaryKey(regionId);
                regionalMaintenance.setRegionalMaintenanceId(UniqueUtil.uuid());
                regionalMaintenance.setCreateTime(DateUtil.DateToString(new Date(), DateStyle.YYYY_MM_DD_HH_MM));
                regionalMaintenance.setModifyTime(DateUtil.DateToString(new Date(), DateStyle.YYYY_MM_DD_HH_MM));
                regionalMaintenance.setRegionId(region.getRegionId());
                regionalMaintenance.setRegionName(region.getRegionName());
                regionalMaintenance.setStatus(Short.parseShort("0"));
                // 废弃了上下级关系， 无法用树形展示
                regionalMaintenance.setOrgId(orgId);
                regionalMaintenance.setRegionType(region.getRegionType());
                regionalMaintenance.setCreatorId(employee.getEmployeeId());
                regionalMaintenance.setModifierId(employee.getEmployeeId());
                resInt = resInt + regionalMaintenanceMapper.insertSelective(regionalMaintenance);
            }
        }
        return resInt;
    }

    /**
     * 批量删除
     * @param regionIds
     * @param employee
     */
    @Override
    public int delRegionalMaintenanceList(List<String> regionIds, Employee employee) throws Exception {
        int resInt = 0 ;
        if(regionIds!=null && regionIds.size() > 0){
            for(String regionalMaintenanceId : regionIds){
                resInt = resInt +  regionalMaintenanceMapper.deleteByPrimaryKey(regionalMaintenanceId);
            }
        }
        return resInt ;
    }

    @Override
    public List<RegionalMaintenance> selectRegionalMaintenanceList(String key) throws Exception {
        return null;
    }

    /**
     * @param regionalMaintenance
     */
    @Override
    public JSONObject selectRegionListJson(RegionalMaintenance regionalMaintenance) throws Exception {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("total", regionalMaintenanceMapper.selectRegionCount(regionalMaintenance)); // selectRegionalMaintenanceCount
        jsonObject.put("rows", JSON.toJSON(regionalMaintenanceMapper.selectRegionList(regionalMaintenance))); // selectRegionalMaintenanceList
        return jsonObject;
    }

    /**
     *
     * @param regionalMaintenance
     * @return
     * @throws Exception
     */
    @Override
    public JSONObject selectRegionalMaintenanceList(RegionalMaintenance regionalMaintenance) throws Exception {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("total", regionalMaintenanceMapper.selectRegionalMaintenanceCount(regionalMaintenance)); // selectRegionalMaintenanceCount
        jsonObject.put("rows", JSON.toJSON(regionalMaintenanceMapper.selectRegionalMaintenanceList(regionalMaintenance))); // selectRegionalMaintenanceList
        return jsonObject;
    }

    /**
     * @param regionalMaintenance 查询当区域 或者 业务体系 或者生产单元下是否存在子区域
     * @return
     * @throws Exception
     */
    @Override
    public List<RegionalMaintenance> selectChildRegionalMaintenanceList(RegionalMaintenance regionalMaintenance) throws Exception {
        return regionalMaintenanceMapper.selectChildRegionalMaintenanceList(regionalMaintenance);
    }

    /**
     * @param regionalMaintenanceId
     * @return
     * @throws Exception
     */
    @Override
    public List<Ztree> selectChildRegionalMaintenanceZtree(String regionalMaintenanceId) throws Exception {
        RegionalMaintenance regionalMaintenance = new RegionalMaintenance();
        regionalMaintenance.setParentRegionId(regionalMaintenanceId);
        List<RegionalMaintenance> regionalMaintenances = regionalMaintenanceMapper.selectChildRegionalMaintenanceList(regionalMaintenance);
        return regionalMaintenanceToZtree(regionalMaintenances);
    }

    /**
     * 将查询区域问题
     */
    @Override
    public List<Ztree> regionalMaintenanceToZtree(List<RegionalMaintenance> regionalMaintenances) throws Exception {
        List<Ztree> ztreeList = new ArrayList<>();
        if (!StringUtils.isEmpty(regionalMaintenances)) {
            for (RegionalMaintenance regionalMaintenance : regionalMaintenances) {
                Ztree ztree = new Ztree();
                ztree.setId(regionalMaintenance.getRegionalMaintenanceId());
                ztree.setpId(regionalMaintenance.getParentRegionId());
                ztree.setName(regionalMaintenance.getRegionName());
                ztree.setIconSkin(offsetSkin(regionalMaintenance.getRegionType().toString()));
                RegionalMaintenance regionalMaintenanceFlag = new RegionalMaintenance();
                regionalMaintenanceFlag.setParentRegionId(regionalMaintenance.getRegionalMaintenanceId());
                List<RegionalMaintenance> regionalMaintenanceFlagList = regionalMaintenanceMapper.selectChildRegionalMaintenanceList(regionalMaintenanceFlag);
                // 判断当前区域下是否存在子区域
                if ( regionalMaintenanceFlagList != null && regionalMaintenanceFlagList.size() == 0) {
                    ztree.setIsParent(false);
                } else {
                    ztree.setIsParent(true);
                }
                ztreeList.add(ztree);
            }
        }
        return ztreeList;
    }

    /**
     * @param regionType
     * @return
     */
    public String offsetSkin(String regionType){
        String reSkin = "" ;
        if(regionType.equals("0")){
            reSkin = TreeIconSkin.getDes("14");
        }else if(regionType.equals("1")){
            reSkin = "d_p_area";
        }else if(regionType.equals("2")){
            reSkin = "process_unit";
        }
        return  reSkin ;
    }

    /**
     * @param orgId 组织机构id
     * @return 获取当前组织机构下的区域信息
     * @throws Exception
     */
    @Override
    public List<RegionalMaintenance> ajaxLoadRegionalMaintenanceList(String orgId) throws Exception {
        return regionalMaintenanceMapper.queryRegionalMaintenanceList(orgId);
    }
}
